#include "button.h"
#include "stm32f4xx_it.h"





/* Private variables prototypes ----------------------------------------------*/
extern unsigned char  Dly10msF;
/* Private variables ---------------------------------------------------------*/
Button_t ButtonObj;
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */





void ButtonInit(void *me)
{
    Button_t *cthis;
    cthis = (Button_t *)me;
    
    cthis->status = enReleased;
    cthis->cnt = 0;
    cthis->tempcnt = 0;
    cthis->action = ButtionAction;
    //cthis->init = ButtonInit;
}


void ButtionAction(void *me)
{
    Button_t *cthis;
    cthis = (Button_t *)me;
    
    if(Dly10msF == 1)
    {
    	Dly10msF = 0;
        switch(cthis->status)
        {
            case enReleased:
                if(Bit_SET == STM_EVAL_PBGetState(BUTTON_USER))
                {
                    cthis->status = enPush;
                }
                else
                {
                    cthis->status = enReleased;
                    cthis->cnt = 0;
                }
                break;
            case enPush:
                if(Bit_SET == STM_EVAL_PBGetState(BUTTON_USER))
                {
                    cthis->status = enPressed;
                    cthis->cnt = 0;
                }
                else
                {
                    cthis->status = enReleased;
                    printf("enReleased\r\n");
                    cthis->cnt = 0;
                }
                break;
            case enPressed:
                if(Bit_SET == STM_EVAL_PBGetState(BUTTON_USER))
                {
                	printf("enPressed\r\n");
                	cthis->tempcnt++;
                	if(cthis->tempcnt>=200)
                	{
                		cthis->tempcnt = 0;
                		cthis->status = enLongPressed;
                	}

                }
                else
                {
                    cthis->status = enReleased;
                    printf("enReleased\r\n");
                    cthis->cnt = 0;
                }
                break;
            case enLongPressed:
                if(Bit_SET == STM_EVAL_PBGetState(BUTTON_USER))
                {
                    cthis->status = enLongPressed;
                    printf("enLongPressed\r\n");
                    cthis->cnt++;
                }
                else
                {  
                    cthis->status = enReleased;
                    printf("enReleased\r\n");
                    cthis->cnt = 0;
                }
                break;
            default :
                break;
        }
    }
}
